package controllers

import (
	"github.com/astaxie/beego"
	"jiabo/appframework/libs/accountandrole"
	"jiabo/appframework/libs/common"
)

type RegisterController struct {
	beego.Controller
}

func (this *RegisterController) Get() {
	this.Ctx.Request.ParseForm()
	// todo := this.Ctx.Request.Form.Get("todo")
	// if todo == "logout" {
	// 	sess := this.StartSession()
	// 	sess.Delete("username")
	// }

	this.TplNames = "login.html"
}

func (this *RegisterController) Post() {
	Username := this.GetString("username")
	Password := this.GetString("password")
	Fullname := this.GetString("fullname")
	Email := this.GetString("email")
	Address := this.GetString("address")
	City := this.GetString("city")
	Country := this.GetString("country")
	md5password := common.MD5(Password)
	account := accountandrole.Account{}
	account.UserName = Username
	account.Password = md5password
	account.Fullname = Fullname
	account.Email = Email
	account.Address = Address
	account.City = City
	account.Country = Country

	ifAccountExist, _ := account.CheckIfAccountExist()
	if ifAccountExist {
		this.Ctx.Redirect(302, "/message?msg=accountexist")
	} else {
		err := account.Save()
		if err != nil {
			this.Ctx.Redirect(302, "/message?msg="+err.Error())
		} else {
			this.Ctx.Redirect(302, "/message?msg=registersucess")
		}
	}
}
